"""
Case Type   : 基础功能-兼容B库语法-optimize
Case Name   : optimize表时，用户没有权限，合理提示
Create At   : 2024/10/25
Owner       : peilinqian
Description :
    step1:创建测试表并插入数据、删除数据
    step2:创建普通用户
    step3:普通用户执行optimize
Expect      :
    step1:创建测试表并插入数据、删除数据，expect: 成功
    step2:创建普通用户 expect:成功
    step3:普通用户执行optimize;expect:提示only table or database owner can vacuum it
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)}:初始化----')
        self.pri_sh = CommonSH('PrimaryDbBmode')
        self.constant = Constant()
        self.com = Common()
        self.t_name = 't_compatibility_optimize_0001'
        self.u_name = 'u_compatibility_optimize_0001'

    def test_main(self):
        step_txt = '----step1:创建测试表并插入数据、删除数据 expect: 成功----'
        self.log.info(step_txt)
        sql = f"drop table if exists {self.t_name};" \
              f"create table {self.t_name}(c1 int,c2 text);" \
              f"create index test_index_001 on {self.t_name}(c1);" \
              f"insert into {self.t_name} values " \
              f"(generate_series(1,10000),'a');" \
              f"delete from {self.t_name};"
        result = self.pri_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG, result,
                      '执行失败:' + step_txt)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, result,
                      '执行失败:' + step_txt)

        step_txt = '----step2:创建普通用户 expect:成功----'
        self.log.info(step_txt)
        sql = f"create user {self.u_name} password 'Test@123';"
        result = self.pri_sh.execut_db_sql(sql)
        self.log.info(result)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, result,
                      '执行失败:' + step_txt)

        step_txt = '----step3:普通用户执行optimize ' \
                   'expect:提示only table or database owner can vacuum it----'
        self.log.info(step_txt)
        sql = f'optimize verbose local table {self.t_name};'
        result = self.pri_sh.execut_db_sql(sql, f'-U {self.u_name} -W Test@123')
        self.log.info(result)
        self.assertIn(f'INFO:  skipping "{self.t_name}" --- '
                      f'only table or database owner can vacuum it', result,
                      '执行失败:' + step_txt)
        self.assertIn(self.constant.VACUUM_SUCCESS_MSG, result,
                      '执行失败:' + step_txt)

    def tearDown(self):
        self.log.info('----this is tearDown----')
        step_txt = '----step:清理数据; expect:清理成功----'
        self.log.info(step_txt)
        drop_sql = f"drop table if exists {self.t_name};" \
                   f"drop user if exists {self.u_name};"
        result = self.pri_sh.execut_db_sql(drop_sql)
        self.log.info(result)

        self.log.info('----teardown断言----')
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, result,
                      "执行失败" + step_txt)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, result,
                      "执行失败" + step_txt)
        self.log.info(f'----{os.path.basename(__file__)}:执行完毕----')
